package com.jeez.common.jpa.service;

import com.jeez.common.entity.DataEntity;
import com.jeez.common.jpa.BaseJpaRepository;
import com.jeez.common.service.BaseService;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * @author: Felix
 * @date: 2018-7-14
 * @version: v1.0
 * @description:
 */
@Transactional
public class JpaCurdService<ID extends Serializable, D extends BaseJpaRepository<T, ID>, T extends DataEntity<T>> extends BaseService {
    /**
     * 持久层对象
     */
    @Autowired
    @Getter
    protected D respository;

    /**
     * 获取单条数据
     * @param id
     * @return
     */
    public T get(ID id) {
        return respository.getOne(id);
    }

    /**
     * 保存数据（插入或更新）
     * @param entity
     */
    @Transactional(readOnly = false)
    public T save(T entity) {
        return this.respository.save(entity);
    }

    /**
     * 修改数据
     * @param entity
     */
    public void update(T entity) {
        respository.update(entity);
    }

    /**
     * 删除数据
     * @param entity
     */
    @Transactional(readOnly = false)
    public void delete(T entity) {
        respository.delete(entity);
    }

    /**
     * 逻辑删除
     * @param entity
     */
    public void logicDelete(T entity) {
        entity.setDelFalg("1");
        respository.logicDelete(entity);
    }

    /**
     * 逻辑删除
     * @param id
     */
    public void logicDelete(ID id) {
        T entity = respository.getOne(id);
        entity.setDelFalg("1");
        respository.update(entity);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteByIds(Iterable<ID> ids) {
        respository.deleteByIds(ids);
    }


}
